package leetcode;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class LeetCodeMain125 {
    public int maxProduct(String[] words) {
        int max=0;
        int[] res=new int[words.length];
        for (int i = 0; i < words.length; i++) {
            String word=words[i];
            int length=word.length();
            for (int j = 0; j < length; j++) {
                res[i]|=1<<(word.charAt(j)-'a');
            }
        }
        for (int i = 0; i < words.length-1; i++) {
            for (int j = i+1; j < words.length; j++) {
                if ((res[i]&res[j])==0){
                    max=Math.max(max,words[i].length()*words[j].length());
                }
            }
        }
        return max;
    }
    public int maxProduct1(String[] words) {
        int  max=0;
        for (int i = 0; i < words.length-1; i++) {
            Set<Character> set=new HashSet<>();
            for (char ch:words[i].toCharArray()) {
                set.add(ch);
            }
            for (int j = i+1; j < words.length; j++) {
                boolean flag=true;
                for (char ch:words[j].toCharArray()) {
                    if (set.contains(ch)){
                        flag=false;
                        break;
                    }
                }
                if (flag){
                    max=Math.max(max,words[i].length()*words[j].length());
                }
            }
        }
        return max;
    }
}
